<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        style="width: 200px;"
        class="filter-item"
        placeholder="消息id"
        v-model="queryForm.msgId"
      >
      </el-input>
      <el-select
        clearable
        style="width: 200px"
        class="filter-item"
        v-model="queryForm.status"
        placeholder="请选择消息状态"
      >
        <el-option
          v-for="(val, key) in Dicts.MESSAGE_IS_READ"
          :key="key"
          :label="val"
          :value="key"
        >
        </el-option>
      </el-select>
      <el-input
        style="width: 200px;"
        class="filter-item"
        placeholder="操作用户"
        v-model="queryForm.name"
      >
      </el-input>
      <el-input
        style="width: 200px;"
        class="filter-item"
        placeholder="设备编号"
        v-model="queryForm.deviceCode"
      >
      </el-input>
      <el-select
        clearable
        style="width: 200px"
        class="filter-item"
        v-model="queryForm.type"
        placeholder="请选择消息类型"
      >
        <el-option
          v-for="(val, key) in Dicts.MODULE"
          :key="key"
          :label="val"
          :value="key"
        >
        </el-option>
      </el-select>
      <el-button
        class="filter-item"
        @click="doQuery"
        type="primary"
        icon="el-icon-search"
        >查询</el-button
      >
    </div>
    <el-table v-loading="loading" :data="records" style="width: 100%">
      <el-table-column prop="rowId" label="id" width="150" align="center">
      </el-table-column>
      <el-table-column prop="msgId" label="消息id" width="150" align="center">
      </el-table-column>
      <el-table-column prop="name" label="操作用户" width="150" align="center">
      </el-table-column>
      <el-table-column prop="type" label="消息类型" width="150" align="center">
        <template slot-scope="scope">
          {{ Dicts.MODULE[scope.row.type] }}
        </template>
      </el-table-column>
      <el-table-column
        prop="deviceCode"
        label="操作设备号"
        width="150"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="status"
        label="消息状态"
        width="200"
        align="center"
      >
        <template slot-scope="scope">
          {{ Dicts.MESSAGE_IS_READ[scope.row.status] }}
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间"
        width="250"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="modifyTime"
        label="更新时间"
        width="250"
        align="center"
      >
      </el-table-column>
      <el-table-column
        align="center"
        label="操作"
        class-name="small-padding"
        fixed="right"
        width="250"
      >
        <template slot-scope="scope">
          <el-button
            v-permission="'messageLog:detail'"
            size="mini"
            type="warning"
            @click="handleOpenMonitor(scope.row.rowId)"
            title="历史"
            icon="el-icon-view"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryForm.current"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="queryForm.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="queryForm.total"
      >
      </el-pagination>
    </div>
    <el-dialog title="日志详情" :visible.sync="dialogDetailVisible" width="30%">
      <el-form :model="detailForm" label-width="80px" ref="detailForm">
        <el-row>
          <el-col :span="12">
            <el-form-item label="标题:" prop="title">
              {{ detailForm.title }}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item
              label="内容:"
              prop="content"
              style="word-wrap: break-word"
            >
              {{ detailForm.content }}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogDetailVisible = false">关闭</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="设备消息列表"
      :visible.sync="dialogCurveVisible"
      :append-to-body="true"
      width="50%"
      height="300"
    >
      <el-table
        :data="tableData"
        stripe
        max-height="400"
        style="height:400px;width:755px"
      >
        <el-table-column prop="msgId" label="消息ID" width="150" align="center">
        </el-table-column>
        <el-table-column
          prop="type"
          label="消息类型"
          width="150"
          align="center"
        >
          <template slot-scope="scope">
            {{ Dicts.MODULE[scope.row.type] }}
          </template>
        </el-table-column>
        <el-table-column
          prop="status"
          label="消息状态"
          width="150"
          align="center"
        >
          <template slot-scope="scope">
            {{ Dicts.MESSAGE_IS_READ[scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column
          prop="name"
          label="操作用户"
          width="150"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="操作日期"
          width="150"
          align="center"
        >
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="closeDialog">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { queryMqttMessage, detailMessageLog, MsgMqttFlow } from '@/api/message'

// function storeLocalStore (permission) {
//     window.sessionStorage.setItem("userMsg",JSON.stringify(permission));
// }
// function document.onkeydown()//屏蔽键盘F5
// {
//   if (window.event.keyCode == 116) {
//     window.event.keyCode = 0;
//     event.cancelBubble = true;
//     return false;
//   }
// }
export default {
  name: 'messageLog',
  data() {
    return {
      loading: false,
      dialogDetailVisible: false,
      dialogCurveVisible: false,
      queryForm: {
        current: 1,
        pages: 0,
        total: 0,
        size: 10,
        status: '',
        msgId: '',
        deviceCode: '',
        name: '',
        type: ''
      },
      detailForm: {
        title: undefined,
        nickname: undefined,
        phone: undefined,
        content: undefined,
        type: undefined,
        targetType: undefined,
        kind: undefined,
        isDrill: undefined,
        isRead: undefined,
        param: undefined,
        drillType: undefined,
        publishTime: undefined,
        status: undefined,
        createBy: undefined,
        createTime: undefined,
        modifyBy: undefined
      },
      typeOptions: Dicts.PUSH_STATUS,
      records: [],
      rowIdForm: {},
      tableData: [],
      permission: { aa: "'messageLog:detail'" }
    }
  },
  created() {
    this.doQuery()
  },
  methods: {
    handleCurrentChange(current) {
      this.queryForm.current = current
      this.doQuery()
    },
    handleSizeChange(size) {
      this.queryForm.size = size
      this.doQuery()
    },
    aa() {},
    doQuery() {
      this.loading = true
      queryMqttMessage(this.queryForm).then(({ data: { data } }) => {
        this.queryForm.current = data.current
        this.queryForm.pages = data.pages
        this.queryForm.total = data.total
        this.queryForm.size = data.size
        this.records = data.records
        this.loading = false
      })
      // storeLocalStore (permission)
    },
    handleOpenMonitor(rowId) {
      // 打开弹窗
      this.dialogCurveVisible = true
      // 拿到设备编号deviceCode
      this.rowIdForm.rowId = rowId
      MsgMqttFlow(this.rowIdForm).then(({ data: { data } }) => {
        //var data_1 = data
        this.tableData = data
      })
    },
    closeDialog() {
      this.dialogCurveVisible = false
      this.tableData = []
      // console.log(this.dataCurve)
    },
    detailMessageLog(rowId) {
      detailMessageLog(rowId).then(({ data: { code, data, msg } }) => {
        this.openDetailDialog()
        this.detailForm = {
          title: data.title,
          content: data.content,
          type: data.type,
          targetType: data.targetType,
          kind: data.kind,
          isDrill: data.isDrill,
          param: data.param,
          publishTime: data.publishTime,
          status: data.status,
          createBy: data.createBy,
          createTime: data.createTime,
          modifyBy: data.modifyBy
        }
      })
    },
    openDetailDialog() {
      this.detailForm = {}
      this.dialogDetailVisible = true
      this.$refs.detailForm && this.$refs.detailForm.resetFields()
    }
  },
  components: {}
}
</script>

<style scoped></style>
